package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysProject;
import com.ruoyi.system.domain.SysProjectDTO;
import com.ruoyi.system.service.ISysProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * 专家抽取Controller
 * 
 * @author ruoyi
 * @date 2024-08-03
 */
@RestController
@RequestMapping("/system/projectextract")
public class SysProjectExtractController extends BaseController
{
    @Autowired
    private ISysProjectService sysProjectService;

    /**
     * 查询项目管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:projectextract:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysProject sysProject)
    {
        startPage();
        List<SysProject> list = sysProjectService.selectSysProjectList(sysProject);
        return getDataTable(list);
    }

    /**
     * 导出项目管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:projectextract:export')")
    @Log(title = "项目管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysProject sysProject)
    {
        List<SysProject> list = sysProjectService.selectSysProjectList(sysProject);
        ExcelUtil<SysProject> util = new ExcelUtil<SysProject>(SysProject.class);
        util.exportExcel(response, list, "项目管理数据");
    }

    /**
     * 获取项目管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:projectextract:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysProjectService.selectSysProjectById(id));
    }

    /**
     * 新增项目管理
     */
    @PreAuthorize("@ss.hasPermi('system:projectextract:add')")
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysProjectDTO SysProjectDTO)
    {

        int isProjectCode  = SysProjectDTO.getIsProjectCode();
        if(isProjectCode == 2) {
            // 生成项目号逻辑
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            String projectCode = dateFormat.format(new Date());
            SysProjectDTO.setProjectCode(projectCode);
        }
        //设置项目状态
        SysProjectDTO.setProjectStatus(1);

        return toAjax(sysProjectService.insertSysProject(SysProjectDTO));
    }

    /**
     * 修改项目管理
     */
    @PreAuthorize("@ss.hasPermi('system:projectextract:edit')")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysProject sysProject)
    {
        return toAjax(sysProjectService.updateSysProject(sysProject));
    }

    /**
     * 删除项目管理
     */
    @PreAuthorize("@ss.hasPermi('system:projectextract:remove')")
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysProjectService.deleteSysProjectByIds(ids));
    }
}
